package Constructions;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URLClassLoader;

import javax.imageio.ImageIO;

import FrameWork.Direction;
import Main.SpaceGame;
import Menus.FireMenu;
import World.TileType;

public class TorpedoBay extends Construction
{
	public int cooldown = 0;
	public boolean canFire;
	Direction orientation;
	BufferedImage spriteReady = null, spriteFired = null;
	
	public TorpedoBay(int x, int y, SpaceGame main) 
	{
		super(x, y, main);
		this.x = x;
		this.y = y;
		this.main = main;
		height = 1; width = 1;
		workingTile = main.world.tiles[x][y];
		canFire = true;
		checkOrientation();
		
		try {
			spriteReady = ImageIO.read(URLClassLoader.getSystemResource("Resources/"+"TorpedoBay.png"));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		try {
			spriteFired = ImageIO.read(URLClassLoader.getSystemResource("Resources/"+"TorpedoBayFired.png"));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		setText();
	}
	
	@Override
	public void draw(Graphics g) 
	{
		BufferedImage sprite = null;
		if(canFire)
			sprite = spriteReady;
		else
			sprite = spriteFired;
		
		double rotationRequired = Math.toRadians(orientation.getRotationRequired());
		double locationX = sprite.getWidth() / 2;
		double locationY = sprite.getHeight() / 2;
		AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
		AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
		
		g.drawImage(op.filter(sprite, null), x*10, y*10, null);
		drawFlash(g, x, y);
	}
	
	@Override
	public void update()
	{
		if(cooldown > 0)
			cooldown--;
		if(cooldown > 0)
			canFire = false;
		else
			canFire = true;
		
	}

	@Override
	public void keyTyped(KeyEvent e) 
	{
		
	}

	@Override
	public void keyPressed(KeyEvent e) 
	{
		if(e.getKeyCode() == KeyEvent.VK_F)
		{
			main.menuManager.addMenu(new FireMenu(main, this));
		}
	}

	@Override
	public void keyReleased(KeyEvent e) 
	{
		
	}

	@Override
	public void setText() 
	{
		textColorIndex = 2;
		
		colors.add(Color.cyan);
		colors.add(Color.cyan);
		text.add("");
		text.add("Torpedo Bay");
		
		colors.add(Color.cyan);
		colors.add(Color.cyan);
		text.add("");
		text.add("");
		
		colors.add(Color.green);
		text.add("f");
		colors.add(Color.cyan);
		text.add("- Fire a torpedo");
	}

	@Override
	public String getName() 
	{
		return "Torpedo Bay";
	}
	
	public void checkOrientation()
	{
		try
		{
			if(main.world.tiles[x-1][y].getType() == TileType.Interior && main.world.tiles[x+1][y].getType() == TileType.Space)
				orientation = Direction.right;
			else if(main.world.tiles[x+1][y].getType() == TileType.Interior && main.world.tiles[x-1][y].getType() == TileType.Space)
				orientation = Direction.left;
			else if(main.world.tiles[x][y+1].getType() == TileType.Interior && main.world.tiles[x][y-1].getType() == TileType.Space)
				orientation = Direction.up;
			else if(main.world.tiles[x][y-1].getType() == TileType.Interior && main.world.tiles[x][y+1].getType() == TileType.Space)
				orientation = Direction.down;
			else
				orientation = Direction.right;
		}
		catch(ArrayIndexOutOfBoundsException e)
		{}
	}
}
